﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Xml;

/// <summary>
/// 返回結果
/// </summary>
[DataContract]
class Weather_Result
{
    [DataMember(Order = 0)]
    public string condition { set;get; }
    [DataMember(Order = 1)]
    public string temp_c { set; get; }
    [DataMember(Order = 2)]
    public string humidity { set; get; }
    [DataMember(Order = 3)]
    public string icon { set; get; }
    [DataMember(Order = 4)]
    public string wind_condition { set; get; }
}
public partial class Service_Weather : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request.QueryString["lat"]) && !string.IsNullOrEmpty(Request.QueryString["lat"]))
        {
            Weather_Result w = new Weather_Result();
            string lat = Request.QueryString["lat"];
            string lon = Request.QueryString["lng"];
            lat = (Convert.ToDouble(lat) * 1000000).ToString();
            lon = (Convert.ToDouble(lon) * 1000000).ToString();
            lat = lat.Replace(".", "");
            if (lat.Length >= 8)
            {
                lat = lat.Substring(0, 8);
            }
            lon = lon.Replace(".", "");
            if (lon.Length >= 9)
            {
                lon = lon.Substring(0, 9);
            }
            XmlDocument dom = new XmlDocument();
            dom.XmlResolver = null;
            dom.Load(string.Format("http://www.google.com/ig/api?weather=,,,{0},{1}", lat, lon));
            try
            {
                w.temp_c = dom.SelectSingleNode("/xml_api_reply/weather/current_conditions/temp_c").Attributes[0].InnerText.ToString();
                w.condition = changeWeatherInfo(dom.SelectSingleNode("/xml_api_reply/weather/current_conditions/condition").Attributes[0].InnerText.ToString());
                w.humidity = dom.SelectSingleNode("/xml_api_reply/weather/current_conditions/humidity").Attributes[0].InnerText.ToString();
                w.icon = dom.SelectSingleNode("/xml_api_reply/weather/current_conditions/icon").Attributes[0].InnerText.ToString();
                w.wind_condition = dom.SelectSingleNode("/xml_api_reply/weather/current_conditions/wind_condition").Attributes[0].InnerText.ToString();
            }
            catch (Exception ex)
            {

            }
            dom = null;
            Response.Write(string.Format("{{\"current_conditions\":{0}}}", MyDb.JSON.stringify(w)));
        }
    }
    public string changeWeatherInfo(string isGood)
    {
        string result = "多雲";
        switch (isGood)
        {
            case "Partly Cloudy":
                result= "局部有雲";
                break;
            case "Clear":
                result= "晴天";
                break;
            case "Cloudy":
                result= "多雲";
                break;
            case "Mostly Cloudy":
                result= "多雲時陰";
                break;
            case "Overcast":
                result= "陰天";
                break;
            case "Haze":
                result= "霾";
                break;
            case "Light rain":
                result= "小雨";
                break;
            case "Thunderstorm":
                result= "雷雨";
                break;
            case "Rain":
                result= "下雨";
                break;
            default:
                //info.isGood = "晴";
                break;
        }
        return result;
    }
}
